#include "TK.h"

int general(int *data,int n,int value){
	int low = 0;
	int high = n-1;
	int mid;
	while(low <= high)
	{	
	        mid = low+((high-low)>>1);
		if(data[mid] == value)
		{
			/*if(mid == 0 || data[mid-1] < value) return mid;
			else high = mid-1;*/
			if(mid == n-1 || data[mid+1] > value) return mid;
			else low = mid+1;
//			return mid;
		}
		else if(data[mid] < value)
		low = mid+1;
		else
		high = mid-1;
	}
	return mid;
}

int recursion(int *data,int n,int value){
//***************equation :mid = (low+high)/2 
//*****terminal condition :low > high
	return cur(data,0,n-1,value);
}
int cur(int *data,int low,int high,int value){
	        int mid = low+((high-low)>>1);
		if(data[mid] == value)
		return mid;
		else if(data[mid] < value)
		return cur(data,mid+1,high,value);
		else
		return cur(data,low,mid-1,value);
}

double compute(int val, int decimal){
	double mid = val/2.0;
	while(fabs(mid*mid-val) > pow(10,decimal*(-1.0)))
	{
		if(mid*mid == (double)val)
		return mid;
		else if(mid*mid < (double)val)
		mid += mid/2.0;
		else
		mid /= 2.0;
	}
	return mid;
}
